package com.ruoyi.accounting.validator;

import com.ruoyi.accounting.mapper.AccAccountBookMapper;
import com.ruoyi.accounting.validator.annotation.ExistBookId;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 账本id校验器
 */
public class ExistBookIdValidator implements ConstraintValidator<ExistBookId, Long> {
    @Autowired
    private AccAccountBookMapper accAccountBookMapper;

    @Override
    public boolean isValid(Long bookId, ConstraintValidatorContext context) {
        if (bookId == null) return true; // 可配合 @NotNull 使用
        return accAccountBookMapper.existsById(bookId) > 0;
    }
}
